function G(id)
{
    return document.getElementById(id);
}

function setIcon(iconName) { 
    var fullPath = iconName;
    try {
        chrome.browserAction.setIcon({path:fullPath});
    } catch(e) {
        console.error("Could not set browser action icon '" + fullPath + "'.");
    }
}

function updateUnreadCount(cnt) {
    unreadCount = "" + cnt;
    if (cnt > 0)
    { 
        chrome.browserAction.setBadgeText({text:unreadCount});
        chrome.browserAction.setBadgeBackgroundColor({color:[170, 80, 80, 255]});
    }
    else if (cnt < 0)
    {
        chrome.browserAction.setBadgeText({text:'x'});
        chrome.browserAction.setBadgeBackgroundColor({color:[170, 80, 80, 80]});
    }
    else
    {
        chrome.browserAction.setBadgeText({text:""});
    }
}

function check_at()
{
    var req = new XMLHttpRequest();
    var url = "http://t.qq.com/asyn/newMsgCount.php?type=4";
    url += "&r=" + (new Date()).toString();
    req.open("GET", url, true);
    req.onreadystatechange = function()
    {
        if (req.readyState == 4 && req.status == 200)
        {
            try{
            eval('var ret = ' + req.responseText + ';');
            if (ret.result == -1 && ret.msg == "\u672a\u767b\u5f55")
            {
                updateUnreadCount(-1);
            }
            else
            {
                for (var i = 0; i < ret.info.length; ++i)
                {
                    if (ret.info[i].type == 4)
                    {
                        var cnt = ret.info[i].value;
                        if (G('cnt'))
                            G('cnt').innerHTML = cnt;
                        updateUnreadCount(cnt);
                        break;
                    }
                }
            }
            }catch(e){}
        }
    };
    req.send(null);
}

function sendTui()
{
    var req = new XMLHttpRequest();
    var url = "http://t.qq.com/publish.php";
    req.open("POST", url, true);
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.setRequestHeader("Referer", "http://t.qq.com/felix021");
    req.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Tablet PC 2.0; .NET4.0C; .NET4.0E)");
    req.onreadystatechange = function()
    {
        if (req.readyState == 4)
        {
            var msg = G('msg');
            if (req.status == 200)
            {
                eval('var ret = ' + req.responseText + ';');
                msg.innerHTML = req.responseText;
                //msg.innerHTML = ret.msg;
            }
            else
            {
                msg.innerHTML = 'Bad request: ' + req.status;
            }
        }
    };
    var tui = G('tui');
    var c = "content=" + encodeURIComponent(tui.value);
    c += '&pic=&countType=&viewModel=1';
    req.send(c);
}

